package com.beadhouse.rolemanage.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.customer.ao.CustomerAo;
import com.beadhouse.customer.service.CustomerService;
import com.beadhouse.entity.CustomerEntity;
import com.beadhouse.usermanage.dto.NurListDto;
import com.beadhouse.usermanage.service.UserManageService;
import com.github.pagehelper.PageHelper;

@Controller
@Validated
public class RoleManageController {
	@Autowired
	UserManageService ums;
	@Autowired
	CustomerService cs;
	@RequestMapping("/hsz/szhldx/nurlist.do")
	public ModelAndView nurList(String key,String pNo) throws Exception {
		ModelAndView mav=new ModelAndView("nurobjset/szhldx");
		int pageNo=1;
		if(pNo!=null) {
			if(pNo.length()!=0) {
				pageNo=Integer.parseInt(pNo);
			}
		}
		QueryResult<NurListDto> qr=ums.selectNur(key, pageNo);
		mav.addObject("nurInfo",qr);
		return mav;
	}
	@ResponseBody
	@RequestMapping("/api/hsz/szhldx/customerlist")
	public JSONResponse customerList(@Validated CustomerAo ao,String pNo,Errors es) throws Exception {
		if(es.hasErrors()) {
			throw new BeadHouseException(es.getAllErrors().get(0).getDefaultMessage());
		}
		int pageNo=1;
		if(pNo!=null) {
			if(pNo.length()!=0) {
				pageNo=Integer.parseInt(pNo);
			}
		}
		PageHelper.startPage(pageNo, 5);
		List<CustomerEntity> ce=cs.selectCustomerList(ao);
		return JSONResponse.ok(ce);
	}
	@ResponseBody
	@RequestMapping("/api/hsz/szhldx/nurlist")
	public JSONResponse ajaxNurList(String key,String pNo) throws Exception {
		int pageNo=1;
		if(pNo!=null) {
			if(pNo.length()!=0) {
				pageNo=Integer.parseInt(pNo);
			}
		}
		
		QueryResult<NurListDto> ue=ums.selectNur(key, pageNo);
		return JSONResponse.ok(ue);
	}
	
	@RequestMapping("/hsz/szhldx/hldxsz.do")
	public String nurCustomerSet(@NotBlank String nurId,@NotBlank String[] cId) throws Exception {
		for(int i=0;i<cId.length;i++) {
			CustomerAo ao=new CustomerAo();
			ao.setId(cId[i]);
			ao.setUserId(nurId);
			cs.updateCustomer(ao);
		}
		return "forward:/hsz/szhldx/nurlist.do"; 
	}
}
